﻿import kliment.controls.CheckBox;
import kliment.controls.ComboBox;
import kliment.data.Form;
import mx.utils.Delegate;
/**
 * ...
 * @author	Kliment
 * @version	1.1.5
 */
class kliment.data.FormField {
	private static var _selectedTextField:TextField;
	private static var _intervalID:Number;
	private var _target;
	private var _required:Boolean;
	
	public var start_value;
	public var form:Form;
	public var name:String;
	
	public function FormField(target:Object, name:String, start_value, required:Boolean) {
		_target = target;
		this.start_value = start_value;
		_required = required;
		this.name = name;
		_setActions();
		if (start_value != null) {
			value = start_value;
		}
	}
	
	private function _setActions():Void {
		if (_target instanceof TextField) {
			_target.onSetFocus = Delegate.create(this, _onTextFieldSetFocusHandler);
			_target.onKillFocus = Delegate.create(this, _onTextFieldonKillFocusHandler);
		}
	}
	
	private function _onTextFieldSetFocusHandler():Void {
		if (_target.text == start_value)
			_target.text = '';
		Mouse.addListener(this);
		_selectedTextField = _target;
		
		if (Stage['displayState'] == 'fullScreen') {
			_intervalID = setInterval(this, '_toNormalScreen', 10);
		}
	}
	
	private function _onTextFieldonKillFocusHandler():Void {
		if (_target.text == '')
			_target.text = start_value;
		Mouse.removeListener(this);
	}
	
	private function _toNormalScreen():Void {
		clearInterval(_intervalID);
		Stage['displayState'] = 'normal';
		Selection.setFocus(_selectedTextField);
	}
	
	private function onMouseDown():Void {
		var mX:Number = _selectedTextField._xmouse;
		var mY:Number = _selectedTextField._ymouse;
		if (!(mX > 0 && mX < _selectedTextField._width && mY > 0 && mY <  + _selectedTextField._height)) {
			Selection.setFocus(null);
			delete _selectedTextField;
		}
	}
	
	public function get isValid():Boolean {
		if (_required && value == '')
			return false;
		if (value == start_value)
			return false;
		return true;
	}
	
	public function get value() {
		var value;
		if (_target instanceof TextField) {
			var textField:TextField = _target;
			value = textField.text;
		} else if (_target instanceof CheckBox) {
			var checkBox:CheckBox = _target;
			value = checkBox.value;
		} else if (_target instanceof ComboBox) {
			var comboBox:ComboBox = _target;
			value = comboBox.value;
		}
		return value;
	}
	
	public function set value(value):Void {
		if (_target instanceof TextField) {
			var textField:TextField = _target;
			textField.text = String(value);
		} else if (_target instanceof CheckBox) {
			var checkBox:CheckBox = _target;
			checkBox.value = Boolean(value);
		}
	}
	
	public function clear():Void {
		value = start_value;
	}
	
	public function remove():Void {
		clear();
		Mouse.removeListener(this);
		if (_target instanceof TextField) {
			_target.onSetFocus = null;
			_target.onKillFocus = null;
		}
	}
}